package fm.last.api
{
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    
    import fm.last.Agent;
    import fm.last.RestAPI;
    
    final public class LibraryAPI extends RestAPI
    {
        // those in comment are not implemented yet
        public static const LIBRARY_ADD_ALBUM:String   = "library.addAlbum";
        public static const LIBRARY_ADD_ARTIST:String  = "library.addArtist";
        public static const LIBRARY_ADD_TRACK:String   = "library.addTrack";
        public static const LIBRARY_GET_ALBUMS:String  = "library.getAlbums";
        public static const LIBRARY_GET_ARTISTS:String = "library.getArtists";
        public static const LIBRARY_GET_TRACKS:String  = "library.getTracks";
        
        public function LibraryAPI(agent:Agent)
        {
            super(agent);
        }

        /* ********************************************************************/
        

        /** library.addAlbum */
        public function addAlbum(onComplete:Function,
                                  onError:Function,
                                  artist:String,
                                  album:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_ADD_ALBUM;
            urlVars["artist"] = artist;  
            urlVars["album"] = album;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);
        }
        
        /** library.addArtist */
        public function addArtist(onComplete:Function,
                                  onError:Function,
                                  artist:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_ADD_ARTIST;
            urlVars["artist"] = artist;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);
        }
        

        /** library.addTrack */
        public function addTrack(onComplete:Function,
                                  onError:Function,
                                  artist:String,
                                  track:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_ADD_TRACK;
            urlVars["artist"] = artist;
            urlVars["track"] = track;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);
        }
        

        /** library.getAlbums */
        public function getAlbums(onComplete:Function,
                                  onError:Function,
                                  user:String,
                                  page:int=-1,
                                  limit:int=-1):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_GET_ALBUMS;
            urlVars["user"] = user;
            if (page>0)  urlVars["page"]  = page;
            if (limit>0) urlVars["limit"] = limit;                   
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);            
        }
        

        /** library.getArtists */
        public function getArtists(onComplete:Function,
                                  onError:Function,
                                  user:String,
                                  page:int=-1,
                                  limit:int=-1):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_GET_ARTISTS;
            urlVars["user"] = user;
            if (page>0)  urlVars["page"]  = page;
            if (limit>0) urlVars["limit"] = limit;                   
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);            
        }
        

        /** library.getTracks */
        public function getTracks(onComplete:Function,
                                  onError:Function,
                                  user:String,
                                  page:int=-1,
                                  limit:int=-1):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = LibraryAPI.LIBRARY_GET_TRACKS;
            urlVars["user"] = user;
            if (page>0)  urlVars["page"]  = page;
            if (limit>0) urlVars["limit"] = limit;                   
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);            
        }
        
    }
}